package com.dalei.vdsmanager.security.dao;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class UserDaoImpl extends HibernateDaoSupport implements UserDao, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2289087905487292930L;

	@Override
	public void saveUser(User user) {
		// TODO Auto-generated method stub
		getHibernateTemplate().save(user);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> findUsers() {
		// TODO Auto-generated method stub
		String hql = "FROM User ORDER BY createdTime";
		List<User> users = getHibernateTemplate().find(hql);
		return users;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> findUsersByCreator(User creator) {
		// TODO Auto-generated method stub
		String hql = "FROM User WHERE createdBy = ? ORDER BY createdTime";
		List<User> users = getHibernateTemplate().find(hql, creator.getId());
		return users;
	}

	@Override
	public User findUserByUserName(String userName) {
		// TODO Auto-generated method stub
		Criteria criteria = getSession().createCriteria(User.class);
		criteria.add(Restrictions.eq("user", userName));
		User user = (User) criteria.uniqueResult();
		return user;
	}

	@Override
	public User findUserById(int id) {
		// TODO Auto-generated method stub
		User user = (User) getHibernateTemplate().get(User.class, id);
		return user;
	}

	@Override
	public void updateUser(User user) {
		// TODO Auto-generated method stub
		getHibernateTemplate().update(user);
	}

	@Override
	public void deleteUser(User user) {
		// TODO Auto-generated method stub
		getHibernateTemplate().delete(user);
	}
}
